import { ref } from "vue";
import { cloneDeep,map } from "lodash";
import {
  getSystemCpuUsage,
  getSystemDiskUsage,
  getSystemMemoryUsage,
  getTopologyCpuUsage,
  getTopologyDiskUsage,
  getTopologyMemoryUsage,
} from "@/api/topoNodeDetail";
import { basiclineOption, buildLineSeriesData } from "@/utils/chartOption";
import { formatChartTooltip, formatUnit } from "@/utils/format";
export default function useCharts() {
  const cpuOption = ref(basiclineOption);
  const memoryOption = ref(basiclineOption);
  const diskOption = ref(basiclineOption);
  // 折线图数据处理公共方法
  const lineDataHandle = (data, name, unit) => {
    const options = cloneDeep(basiclineOption);
    options.xAxis[0].data = map(data, "time");
    options.legend.data.push(name);
    options.legend.show = false;
    options.grid.top = 15;
    options.yAxis[0].axisLabel.formatter = (v) => formatUnit(v, unit);
    options.series.push(buildLineSeriesData(name, map(data, "value")));
    options.tooltip.formatter = (data) => formatChartTooltip(data, unit);
    return options;
  };
  // 获取CPU使用量Line Data
  const getCpuUsage = (params, apiType) => {
    const promise =
      apiType === "system"
        ? getSystemCpuUsage(params)
        : getTopologyCpuUsage(params);
    promise.then((res) => {
      const option = lineDataHandle(res.data, "CPU使用量", "核");
      cpuOption.value = (option);
    });
  };
  // 获取内存使用量Line Data
  const getMemoryUsage = (params, apiType) => {
    const promise =
      apiType === "system"
        ? getSystemMemoryUsage(params)
        : getTopologyMemoryUsage(params);
    promise.then((res) => {
      const option = lineDataHandle(res.data, "内存使用量", "byteMin");
      memoryOption.value = (option);
    });
  };
  // 获取磁盘使用量Line Data
  const getDiskUsage = (params, apiType) => {
    const promise =
      apiType === "system"
        ? getSystemDiskUsage(params)
        : getTopologyDiskUsage(params);
    promise.then((res) => {
      const option = lineDataHandle(res.data, "磁盘使用量", "byteMin");
      diskOption.value = (option);
    });
  };
  return {
    cpuOption,
    memoryOption,
    diskOption,
    getCpuUsage,
    getMemoryUsage,
    getDiskUsage,
  };
}
